//
//  InterpretationViewController.m
//  MM
//
//  Created by 王晨辉 on 15/5/15.
//  Copyright (c) 2015年 王晨辉. All rights reserved.
//

#import "InterpretationViewController.h"
#import "cityViewController.h"
#import "PayEvaluationViewController.h"
#import "CityChooseVC.h"
#import "ProvinceViewController.h"
#import "interperShiViewController.h"
#import "areaManager.h"
#import "InterpretationTwoViewController.h"
@interface InterpretationViewController ()

@end

@implementation InterpretationViewController

-(void)viewDidAppear:(BOOL)animated{
    
    
    [self.navigationController.navigationBar setBarTintColor:[GetFileModel colorWithHexString:@"#44afd7" alpha:1]];

    if([areaManager shareInstance].areaId ==nil)
    {
        _areaStr =@"选择地区";
        //右边按钮
        UIBarButtonItem *right = [[UIBarButtonItem alloc]
                                  initWithTitle:_areaStr
                                  style:UIBarButtonItemStylePlain
                                  target:self
                                  action:@selector(babys)];
        [right setTintColor:[UIColor whiteColor]];

        self.navigationItem.rightBarButtonItem = right;
        ////NSLog(@"%@",_areaStr);
        
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString *ID = [user objectForKey:@"ID"];
        
        NSDictionary *param44 = @{@"userId":ID};
        [MBProgressHUD showHUDAddedTo:_tableView animated:YES];
        
        [UserDAL getEvaluationDetailItemsParams:param44 withBlock:^(NSArray *data, NSError *error) {

            numberArray = [NSMutableArray array];
            atrArray = [NSMutableArray array];
            timeArray = [NSMutableArray array];
            typeArray  = [NSMutableArray array];
            if (data.count!=0) {
                
                for (NSDictionary * dic in data) {
                    NSString * activityId = [dic objectForKey:@"activityId"];
                    NSString * activityName = [dic objectForKey:@"activityName"];
                    NSString * isJoin = [dic objectForKey:@"isJoin"];
                    NSString * timeStamp = [dic objectForKey:@"timeStamp"];
                    //            ////NSLog(@"%@,%@,%@,%@",position,activityName,isJoin,timeStamp);
                    [numberArray addObject:isJoin];
                    [timeArray addObject:timeStamp];
                    [atrArray addObject:activityName];
                    [typeArray addObject:activityId];
                    ////NSLog(@"%@,%@,%@,",numberArray,timeArray,atrArray);
                    
                    
                }
                
            }else{
                
                [JKAlert showMessage:@"提示" message:@"请选择城市"];
                
                
                
            }
            
            
            [_tableView reloadData];
            
            
            
            
            
        }];
        
        
//        [JKAlert showMessage:@"提示" message:@"请选择城市"];

    }else if([areaManager shareInstance].areaId)
    {
        _areaStr = [NSString stringWithFormat:@"%@"/*,[areaManager shareInstance].areaProvince,[areaManager shareInstance].areaCity*/,[areaManager shareInstance].areaName];
        //右边按钮
        _areaStrT = [NSString stringWithFormat:@"%@",[areaManager shareInstance].areaCity];
        
        ////NSLog(@"%@传个这个给我了",[areaManager shareInstance].areaId);
        
        
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString *ID = [user objectForKey:@"ID"];
        
        NSDictionary *param44 = @{@"userId":ID,@"typeId":@"2",@"location":[areaManager shareInstance].areaId};
        [MBProgressHUD showHUDAddedTo:_tableView animated:YES];
        
        [UserDAL getEvaluationDetailItemsParams:param44 withBlock:^(NSArray *data, NSError *error) {
            [MBProgressHUD hideHUDForView:_tableView animated:YES];

            numberArray = [NSMutableArray array];
            atrArray = [NSMutableArray array];
            timeArray = [NSMutableArray array];
            typeArray  = [NSMutableArray array];
            if (data.count!=0) {
                
                for (NSDictionary * dic in data) {
                    NSString * activityId = [dic objectForKey:@"activityId"];
                    NSString * activityName = [dic objectForKey:@"activityName"];
                    NSString * isJoin = [dic objectForKey:@"isJoin"];
                    NSString * timeStamp = [dic objectForKey:@"timeStamp"];
                    //            ////NSLog(@"%@,%@,%@,%@",position,activityName,isJoin,timeStamp);
                    [numberArray addObject:isJoin];
                    [timeArray addObject:timeStamp];
                    [atrArray addObject:activityName];
                    [typeArray addObject:activityId];
                    ////NSLog(@"%@,%@,%@,",numberArray,timeArray,atrArray);
                    
                    
                }
                
            }else{
                
            

                [JKAlert showMessage:@"提示" message:@"对不起，您所在地区还未有麦苗公学，请及时关注麦苗信息，欢迎加盟咨询。"];
                
                
            }
            
            
            [_tableView reloadData];
            
            
            
            
            
        }];

        
        if ([_areaStr  isEqual: @""]) {
            

            UIBarButtonItem *right = [[UIBarButtonItem alloc]
                                      initWithTitle:_areaStrT
                                      style:UIBarButtonItemStylePlain
                                      target:self
                                      action:@selector(babys)];
            [right setTintColor:[UIColor whiteColor]];
            self.navigationItem.rightBarButtonItem = right;
        } else {
            UIBarButtonItem *right = [[UIBarButtonItem alloc]
                                      initWithTitle:_areaStr
                                      style:UIBarButtonItemStylePlain
                                      target:self
                                      action:@selector(babys)];
            [right setTintColor:[UIColor whiteColor]];
            self.navigationItem.rightBarButtonItem = right;
        }

        ////NSLog(@"%@",_areaStr);
        
        //        [areaManager shareInstance].areaName;
        
    }
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    

    [GetFileModel ShowProgressHUDwithMessage:@"麦苗正在为您飙油门加载中。"];
    //同城解读
    //设置导航栏头文字
    self.navigationItem.title = @"同城解读";
    //设置导航栏背景颜色
    [self.navigationController.navigationBar setBarTintColor:[GetFileModel colorWithHexString:@"#44afd7" alpha:1]];
    //设置导航栏字体颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    //自定义左右按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"_0000_返回.png"]
                      forState:UIControlStateNormal];
    [button addTarget:self action:@selector(turntolast)
     forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 20, 15);
    
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    self.navigationItem.leftBarButtonItem = menuButton;
   self.view.backgroundColor = [UIColor whiteColor];

    [self subView];
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tongzhi:) name:@"tongzhi" object:nil];
    
}
-(void)subView{
    

    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 52;
    _tableView.separatorStyle = UITableViewCellEditingStyleNone;


    
    [self.view addSubview:_tableView];
    

    
    
    
}
-(void)turntolast{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}



- (void)tongzhi:(NSNotification *)text{
    ////NSLog(@"%@",text.userInfo[@"textOne"]);
    ////NSLog(@"－－－－－接收到通知------");
    
    
    
}




-(void)babys{
    
    
    ProvinceViewController * cityVC = [[ProvinceViewController alloc]init];
    [self.navigationController pushViewController:cityVC animated:YES];
    
    
    
    
    
    
    
}

-(void)baby{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
    
    
}
#pragma mark--- tableViewDelegate 每个区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return numberArray.count;
}

#pragma mark--- tableViewDelegate 区尾的高度

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0;
    
}
#pragma mark--- tableViewDelegate cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    [MBProgressHUD hideHUDForView:_tableView animated:YES];
    static NSString *NOTIFY = @"cell";
//    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:NOTIFY];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NOTIFY];
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        _tableView.tableFooterView = [[UIView alloc]init];
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, _tableView.rowHeight-1, SCREEN_WIDTH, 1)];
        view.backgroundColor = UIColorWithRGBA(229, 229, 231, 1);
        [cell addSubview:view];
        
        
        UILabel * typeLab = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, SCREEN_WIDTH/3*2, 30)];
        typeLab.text = [NSString stringWithFormat:@"会场：%@",atrArray[indexPath.row]];
        typeLab.adjustsFontSizeToFitWidth = YES;
        typeLab.font = [UIFont systemFontOfSize:16];
        typeLab.textAlignment = NSTextAlignmentLeft;
        [cell addSubview:typeLab];
        
        
        UILabel * timeLab = [[UILabel alloc]initWithFrame:CGRectMake(5, 30, SCREEN_WIDTH/3*2, 20)];
        timeLab.text = [NSString stringWithFormat:@"时间：%@",timeArray[indexPath.row]];
        typeLab.adjustsFontSizeToFitWidth = YES;
        timeLab.font = [UIFont systemFontOfSize:12];
        timeLab.textAlignment = NSTextAlignmentLeft;
        [cell addSubview:timeLab];

        
        
        
    }
    
    
    

    return cell;
  
    
}
#pragma mark--- tableViewDelegate 有几个区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
    
}
#pragma mark--- tableViewDelegat 点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];


        
//        atrArray[indexPath.row]

        InterpretationTwoViewController * pay = [[InterpretationTwoViewController alloc]init];
        pay.activityId = typeArray[indexPath.row];
        pay.dont = @"0";
        pay.name = atrArray[indexPath.row];
        UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:pay];
        [self presentViewController:nav animated:YES completion:nil];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
